package com.tcgroup.phone.base_core.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.tcgroup.phone.base_core.widget.factory.TabItemViewFactory;
import com.tcgroup.phone.base_core.widget.model.TabItemModel;

import java.util.ArrayList;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/16.
 * 包名: com.tcgroup.phone.base_core.widget
 * 类名: OSAndroidMvvm
 */
public class SmartTabLayout extends TabLayout {

   public ViewPager viewPager = null;
   public TabItemViewFactory tabItemFactory = null;

   public OnSmartTabClickListener onSmartTabClickListener;

    public SmartTabLayout(Context context) {
        super(context);
    }

    public SmartTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SmartTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setViewPager(ViewPager viewPager) {
        this.viewPager = viewPager;
        setupWithViewPager(viewPager);
    }

    @Override
    public void setupWithViewPager(@Nullable ViewPager viewPager) {
        super.setupWithViewPager(viewPager);
    }

    @Override
    public void setupWithViewPager(@Nullable ViewPager viewPager, boolean autoRefresh) {
        super.setupWithViewPager(viewPager, autoRefresh);
    }

    protected void createTabLayout(ArrayList<? extends TabItemModel> list){
        if (getTabCount() == 0){
            int length = list.size();
            for (int i = 0;i < length;i++) {
                TabLayout.Tab tab = newTab().setCustomView(tabItemFactory.inflateView(getContext(), list.get(i)));
                addTab(tab);
                View view = (View) tab.getCustomView().getParent();
                int finalI = i;
                view.setOnTouchListener((v, event) -> {
                    if (onSmartTabClickListener == null){
                        return false;
                    }
                    onSmartTabClickListener.onClick(list.get(finalI),finalI,event);
                    return false;
                });
            }
        } else {
            int length = list.size();
            for (int i = 0; i< length ; i++){
                Tab tab = getTabAt(i);
                if (tab.getCustomView() == null){
                    tab.setCustomView(tabItemFactory.inflateView(getContext(),list.get(i)));
                }
                View view = (View) tab.getCustomView().getParent();
                int finalI = i;
                view.setOnTouchListener((v, event) -> {
                    if (onSmartTabClickListener == null){
                        return false;
                    }
                    onSmartTabClickListener.onClick(list.get(finalI),finalI,event);
                    return false;
                });
            }
        }
    }

    public TabItemViewFactory<TabItemModel> getTabItemFactory() {
        return tabItemFactory;
    }

    public void createItemFactory(TabItemViewFactory tabItemFactory) {
        this.tabItemFactory = tabItemFactory;
    }

    public void createItemModelList(ArrayList<? extends TabItemModel> list) {
        createTabLayout(list);
    }

    public OnSmartTabClickListener getOnSmartTabClickListener() {
        return onSmartTabClickListener;
    }

    public void setOnSmartTabClickListener(OnSmartTabClickListener onSmartTabClickListener) {
        this.onSmartTabClickListener = onSmartTabClickListener;
    }

    public interface OnSmartTabClickListener{
        void onClick(TabItemModel model, int position, MotionEvent event);
    }
}
